<?php
	if(!defined("IN_APP"))
	{
		exit;
	}
	
	if(!adminpermission('Y','100000'))
	{
		echo '无权查看!';
		exit;
	}
	
	$method = getParam('m','get','list');
	
	$parm = requestArrayParm($_REQUEST);
	
	if($method == 'detail')
	{
		if(!adminpermission('Y','100000','100001'))
		{
			exit('无权访问');
		}
		
		
		if(!empty($parm['id']))
		{
			//获取用户
			$info = _User::getUserInfo(array('id'=>$parm['id']));
			if($info)
			{
				$bank = json_decode($info['bank'],true);
				$alipay = json_decode($info['alipay'],true);
				
				$bankName = $bank['bankName'];
				$bankCode = $bank['bankNum'];
				$alipayCode = $alipay['alipayNum'];
				unset($wheres);
	
				$wheres['userid'] = $info['id'];
				
				$wheres['addtime'] =  array('<=',time());
				
				$flye = _SqlTool::getSum('jyinfo', $wheres, array(),'money');
				
				unset($wheres);
			
				$wheres['userid'] = $info['id'];
				
				$between['start'] = strtotime(date('Y-m-d'));
				
				$between['end'] = strtotime(date('Y-m-d'))+86399;
				
				$wheres['money'] = array('>',0);
				
				$jrfl = _SqlTool::getSum('jyinfo', $wheres, $between,'money');
				
				unset($wheres);
			
				$wheres['userid'] = $info['id'];
				
				$wheres['addtime'] =  array('<=',time());
				
				$wheres['money'] = array('>',0);
				
				$ljfl = _SqlTool::getSum('jyinfo', $wheres, array(),'money');
				
				
				include renderTemplate('user/user');
			}
			else
			{
				header("Location:".SITE_URL.'index.php?a=userM&m=list');
				exit;
			}
		}
		else
		{
			header("Location:".SITE_URL.'index.php?a=userM&m=list');
			exit;
		}
	}
	
	if($method == 'list')
	{
		$typearr['name'] = '姓名';
		$typearr['phone'] = '手机号';
		$typearr['source'] = '推荐人/渠道';
		$fTitle = "用户管理";
		$sTitle = "用户列表";
		
		$search = array();
		$bool = true;
		if($parm['start'] && $parm['end'])
		{
			$between['start'] = strtotime($parm['start']);
			
			$between['end'] = strtotime($parm['end']);
		}
		
		if($parm['type'] == 'name')
		{
			$search['nickname'] = $parm['keyword'];
		}
		
		if($parm['type'] == 'phone')
		{
			$search['loginname'] = $parm['keyword'];
		}
		
		if($parm['type'] == 'source')
		{
			$search['source'] = $parm['keyword'];
		}
		
		if(!empty($parm['status']))
		{
			unset($query,$arr);
			$query['status'] = '2';
			$query['status2'] = '2';
			$resData = _SqlTool::getDistinctList('tzinfo',$query,'userid');
			foreach ($resData['list'] as $k => $v)
			{
				$arr[] = $v['userid'];
			}
			
			if($parm['status'] == '1')
			{
				if(!empty($arr))
				{
					$search['id'] = array('in',join(',',$arr));
				}
				else
				{
					$bool = false;
				}
			}
			
			if($parm['status'] == '2')
			{
				if(!empty($arr))
				{
					$search['id'] = array('not in',join(',',$arr));
				}
				else
				{
					$bool = true;
				}
			}
		}
		
		if($bool)
		{
			$page = $pages['page'] = getParam('page','request','1');
		
			$pages['rows'] = 20;
			
			$sort['regtime'] = 'desc';
			
			$resData = _SqlTool::getList('users', $search, $pages, $sort, $between);
			
			//查询对应的数据
			
			foreach ($resData['list']  as $key => $value)
			{
				unset($query);
				$query['userid'] = $value['id'];
				$query['status'] = '2';
				$query['status2'] = '2';
				$count = _SqlTool::getCount('tzinfo', $query);
				if($count > 0)
				{
					$value['touzi'] = '1';
				}
				else
				{
					$value['touzi'] = '2';
				}
				$list[] = $value;
			}
			
			$pageurl = SITE_URL.'index.php?'.set_URL_Param(get_URL_Param());
		
			$pagetotal = $resData["pages"];
		}
		
		include renderTemplate('user/userlist');
	}
	
	